package com.house.rent.ui.activity;

import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.base.lib.mvp.present.BasePresenter;
import com.base.lib.util.InputMethodUtils;
import com.house.rent.R;
import com.house.rent.entity.HouseEntity;
import com.house.rent.ui.adapter.HouseAdapter;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @desc 搜索
 */
public class SearchActivity extends BaseActivity {

    @BindView(R.id.edt_search)
    EditText mEdtSearch;
    @BindView(R.id.rcv_search)
    RecyclerView mRecyclerView;

    HouseAdapter mAdapter;
    private int mType;
    private String mOrderBy = "desc";

    public static void start(Context context) {
        Intent starter = new Intent(context, SearchActivity.class);
        context.startActivity(starter);
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_search;
    }

    @Override
    protected void initTop() {
        enableTop(false);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {

        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        mRecyclerView.setAdapter(mAdapter = new HouseAdapter(mContext));
    }

    @Override
    protected void initListeners() {
        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            HouseDetailActivity.start(mContext, mAdapter.getData().get(position).getId());
        });
        mAdapter.setOnLoadMoreListener(null, mRecyclerView);
    }

    @Override
    protected BasePresenter initPresenter() {
        return null;
    }

    @Override
    protected void initData() {

    }

    private void doSearchRequest() {
        String keywords = mEdtSearch.getText().toString().trim();
        if (TextUtils.isEmpty(keywords)) {
            showMessage("请输入小区名称");
            return;
        }
        addParams("communityName", keywords);
        addParams("orderBy", mOrderBy);
        if (mType >= 0) {
            addParams("type", mType);
        }
        sendRequest("house-info/search", HouseEntity.class);
    }

    @Override
    protected void onRequestSuccess(String reqId, Object obj) {
        super.onRequestSuccess(reqId, obj);
        if (obj instanceof List) {
            List<HouseEntity> entities = (List<HouseEntity>) obj;
            mAdapter.setNewData(entities);
            mAdapter.loadMoreEnd();

            if (entities.isEmpty()) {
                mAdapter.setEmptyView(R.layout.layout_empty);
            }
        }
    }

    @OnClick({R.id.iv_search_back, R.id.tv_search, R.id.tv_search_type, R.id.tv_search_price})
    public void onViewClick(View view) {
        InputMethodUtils.hideInputMethod(this);
        switch (view.getId()) {
            case R.id.iv_search_back:
                finish();
                break;
            case R.id.tv_search:
                doSearchRequest();
                break;
            case R.id.tv_search_type:
                showSelectTypeDialog();
                break;
            case R.id.tv_search_price:
                showSelectPriceDialog();
                break;
            default:
                break;
        }
    }

    private void showSelectTypeDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("请选择搜索类型");
        //类型 0-出售 1-求购 2-求租 3-出租
        final String items[] = {"出售", "求购", "求租", "出租"};
        builder.setSingleChoiceItems(items, -1, (dialog, which) -> {
            mType = which;
            dialog.dismiss();

            doSearchRequest();
        });
        builder.create().show();
    }

    private void showSelectPriceDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("请选择价格排序");
        //价格排序 desc-倒序 asc-正序
        final String items[] = {"从高到低", "从低到高"};
        builder.setSingleChoiceItems(items, -1, (dialog, which) -> {
            switch (which) {
                case 1:
                    mOrderBy = "asc";
                    break;
                default:
                    mOrderBy = "desc";
                    break;
            }
            dialog.dismiss();

            doSearchRequest();
        });
    }

}
